package com.gin.kotlin.no_gay_alliance.provider.ngajs

import com.gin.nga.api.ResourceApi
import com.gin.nga.response.resource.ui.AvatarBuff
import com.gin.nga.response.resource.ui.UiResource
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit

/**
 * 头像buff提供者
 * @property relativePath String
 */
class AvatarBuffProvider private constructor() : AbstractNgaResourceProvider<UiResource, List<AvatarBuff>>(
    javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, AvatarBuff::class.java),
    expires = TimeUnit.DAYS.toSeconds(1)
) {
    override val relativePath = "$DIR/avatarBuff.json"

    /**
     * 请求方法
     * @param resourceApi NgaApi
     * @return Call<NgaResponse<R>>
     */
    override fun requestMethod(resourceApi: ResourceApi) = resourceApi.ui()

    /**
     * 将响应类型转换为缓存类型的方法
     * @param res R
     * @return D
     */
    override fun transform(res: UiResource?) = res?.avatarBuffs

    companion object {
        private var instance: WeakReference<AvatarBuffProvider> = WeakReference(AvatarBuffProvider())

        /**
         * 单例模式+弱引用
         * @return Any
         */
        fun getInstance(): AvatarBuffProvider = instance.get() ?: AvatarBuffProvider().also { instance = WeakReference(it) }
    }
}